capture log close

/*==================================================
project:       Replication code for Kuznetsova, D., & Tolbert, C. (2023). Modelling temporal dynamics: Does internet use fuel anti-government protests? Democratization. https://doi.org/10.1080/13510347.2023.2268019conference submission

----------------------------------------------------  
Input: 			   protest.dta      
Output:             
==================================================*/

version 17
drop _all
estimates clear


*********************
* Figure 1

set scheme cleanplots
graph set window fontface "Arial Narrow"

use protest.dta, clear

merge m:1 country_wb using world.dta
drop _merge

* map of internet penetration rates
graph drop _all

forvalues i = 1990(10)2020 {
	
replace year = `i' in 4744/4841

colorpalette plasma, n(11) nograph reverse	 
local colors `r(p)'

spmap wb_imputed using world_shp2.dta if year == `i', id(_ID) ///
	clm(custom) clb(0 6 12.5 25 37.5 50 62.5 75 87.5 100) ///
	fcolor("`colors'") 	///
	ndocolor(gs5) ndsize(0.05) ndpattern(solid)  ///
	legstyle(2)	legend(pos(7) size(2.8) region(fcolor(gs15))) ///
	ocolor(black ..) osize(0.05 ..)	///
	title("`i'", size(medsmall)) ///
	name(map_`i')
}

graph combine map_1990 map_2000 map_2010 map_2020

graph export "figure1.png", replace width(3000)


*********************
*Figure 2

use protest.dta, clear

set scheme cleanplots

egen int_penet = mean(wb_imputed), by(year dem_cy)	
label variable int_penet "average internet penetration"	
tsline int_penet, by(dem_cy, note("")) 

graph export "fugire2.png", replace width(500)


*********************
* Table 1

use protest.dta, clear

replace mobile_imputed = 200 if mobile_imputed > 200

asdoc sum n wb_imputed mobile_imputed ln_pop ln_gdp pop_20_24_mean v2mecenefi, ///
	by(dem_cy) label save(table1.doc)

* summary statistics for internet penetration rates were generated after removing observations for South Sudan and Somalia (no data for GDP, so the observations for these countries are listwise deleted in the models)
* summary statistics for mobile cellular subscriptions were calculated with a cap on the subscriptions at 200

drop if country =="South Sudan"
drop if country == "Somalia"
	
asdoc sum ln_wb, by(dem_cy) label save(table1.doc)


*********************
* Table 2

use protest.dta, clear
drop if country =="South Sudan"
drop if country == "Somalia"

xtset group year

* internet penetration	
xtnbreg n c.ln_wb##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3
estimates store model1

margins, dydx(ln_wb) at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

	marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) yline(0) ///
	name(leftpane)
	
	graph export "figure3_leftpane.png", replace width(1000)	

xtnbreg n l_n c.ln_wb##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3
estimates store model2

* mobile 
xtnbreg n c.mobile_imputed##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3
estimates store model3

margins, dydx(mobile_imputed) at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

	marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) yline(0) ///
	name(rightpane)
	
	graph export "figure3_rightpane.png", replace width(1000)	

xtnbreg n l_n c.mobile_imputed##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3
estimates store model4

* table 2
esttab model1 model2 model3 model4 using "table2.rtf", b(3) compress ///
		se label replace cells(b(fmt(3) star) ///
		se(par)) ///
		nobaselevels ///
		varwidth(30) collabels(, none)

		
*********************
* Figure 3
	
graph combine leftpane rightpane, ///
scale(1.5) xsize(6) ysize(3) 	
	
	graph export "figure3.png", replace width(1000)	
	
	
*********************
* Table 3:

foreach var of varlist mobile_imputed ln_wb {
xtnbreg n c.`var'##c.v2mecenefi ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if dem_cy == 0

estimates store nb_`var'_nondem
	
margins, dydx(`var') at(v2mecenefi = (-4(0.5)2)) predict(nu0)
marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) yline(0) ///
	name(`var'_nondem)	
}

graph combine mobile_imputed_nondem ln_wb_nondem, ///
scale(1.5) xsize(6) ysize(3)

graph export "figure4.png", replace width(2000)

* table 3
esttab nb_mobile_imputed_nondem nb_ln_wb_nondem using "table3.rtf", b(3) compress ///
		se label replace cells(b(fmt(3) star) ///
		se(par)) ///
		nobaselevels ///
		varwidth(30) collabels(, none)

		
*********************
* Table 4

foreach var in mobile_imputed ln_wb {
* 1990-2010
xtnbreg n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year < 2011		
estimates store nb_5_`var'	

margins, dydx(`var') at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) ///
	name(graph_1_`var', replace)	

xtnbreg n l_n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year < 2011
estimates store nb_6_`var'

* 2011-2020
xtnbreg n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year > 2010		
estimates store nb_7_`var'

margins, dydx(`var') at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) ///
	name(graph_3_`var', replace)
				
xtnbreg n l_n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year > 2010		
estimates store nb_8_`var'
}

* table 4
esttab nb_5_mobile_imputed nb_6_mobile_imputed nb_7_mobile_imputed nb_8_mobile_imputed nb_5_ln_wb nb_6_ln_wb nb_7_ln_wb nb_8_ln_wb using "table4.rtf", b(3) compress ///
		se label replace cells(b(fmt(3) star) ///
		se(par)) ///
		nobaselevels  ///
		order(l_n) ///
		varwidth(30) collabels(, none)	
	
	
*******************	
* Figure 5

graph combine graph_1_mobile_imputed graph_3_mobile_imputed, ///
	scale(1.5) xsize(6) ysize(3)
	
	graph export "figure5.png", replace width(2000)	


*******************	
* Figure 6
	
graph combine graph_1_ln_wb graph_3_ln_wb, ///
	scale(1.5) xsize(6) ysize(3)
	
	graph export "figure6.png", replace width(2000)	
	
	
*************************
* Online Appendix
*************************	

*************************
* 2009 and 2011 cutoff points

foreach var in mobile_imputed ln_wb {
	forvalues i = 2009(1)2011 {
* 1990-i
xtnbreg n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year <= `i'		
estimates store nb_5	

margins, dydx(`var') at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) ///
	name(`var'_`i'_1, replace)	

xtnbreg n l_n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year <= `i'
estimates store nb_6

margins, dydx(`var') at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) ///
	name(`var'_`i'_2, replace)		

* i-2020
xtnbreg n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year > `i'
estimates store nb_7

margins, dydx(`var') at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) ///
	name(`var'_`i'_3, replace)
				
xtnbreg n l_n c.`var'##c.v2x_polyarchy ln_pop ln_gdp pop_20_24_mean y1 y2 y3 if year > `i'	
estimates store nb_8

margins, dydx(`var') at(v2x_polyarchy = (0(0.1)0.9)) predict(nu0)

marginsplot, recast(line) ///
	recastci(rarea) ciopt(color(%40)) ///
	note(Shaded area represents 95% CI) ///
	name(`var'_`i'_4, replace)	
	}
}
	
**************************	
* Figure A1

graph combine mobile_imputed_2009_1 mobile_imputed_2009_2 mobile_imputed_2009_3 mobile_imputed_2009_4, ///
	iscale (0.5) imargin(vsmall)
	
	graph export "figureA1.png", replace width(2000)	

**************************
* Figure A2

graph combine mobile_imputed_2011_1 mobile_imputed_2011_2 mobile_imputed_2011_3 mobile_imputed_2011_4, ///
	iscale (0.5) imargin(vsmall)
	
	graph export "figureA2.png", replace width(2000)	
	

	
**************************	
* Figure A3

graph combine ln_wb_2009_1 ln_wb_2009_2 ln_wb_2009_3 ln_wb_2009_4, ///
	iscale (0.5) imargin(vsmall) title("2009 cutoff (1990-2009 (left) and 2010-2020 (right), models with lags on the right")
	
	graph export "figureA3.png", replace width(2000)	

	
**************************	
* Figure A4

	graph combine ln_wb_2011_1 ln_wb_2011_2 ln_wb_2011_3 ln_wb_2011_4, ///
	iscale (0.5) imargin(vsmall) title("2011 cutoff (1990-2011 (left) and 2012-2020 (right), models with lags on the right")
	
	graph export "figureA4.png", replace width(2000)	
	

********************
* Table A1: stationarity tests

/*

Levin-Lin-Chu test and other panel-data unit-root tests require data to be perfectly balanced to run the test. Therefore, I first impute any missing values and then run the unit root tests for the independent variables. In the models, I only impute the mobile and internet penetration. 

*/

sort country year

gen ln_wb_i = log(wb_imputed + 0.0001)

by country year: ipolate ln_pop year, gen(ln_pop_i) epolate
replace ln_pop_i = ln_pop_i[_n-1] if missing(ln_pop_i)

by country year: ipolate ln_gdp year, gen(ln_gdp_i) epolate
replace ln_gdp_i = ln_gdp_i[_n-1] if missing(ln_gdp_i)
replace ln_gdp_i = ln_gdp_i[_n+1] if missing(ln_gdp_i)
replace ln_gdp_i = 5.1897659 in 1/11 /*missing values for Afganistan) */

by country year: ipolate pop_20_24_mean year, gen(pop_20_24_i) epolate
replace pop_20_24_i = pop_20_24_i[_n-1] if missing(pop_20_24_i)

by country year: ipolate v2x_polyarchy year, gen(polyarchy_i) epolate
replace polyarchy_i = polyarchy_i[_n-1] if missing(polyarchy_i)

by country year: ipolate v2mecenefi year, gen(v2mecenefi_i) epolate
replace v2mecenefi_i = v2mecenefi_i[_n-1] if missing(v2mecenefi_i)
replace v2mecenefi_i = -0.291 in 1/12 /*missing values for Afganistan) */

foreach var of varlist wb_imputed ln_wb_i mobile_imputed ln_pop_i ln_gdp_i pop_20_24_i polyarchy_i v2mecenefi_i {
	xtunitroot llc `var'
	xtunitroot hadri `var'
}

* end of do file

exit